package com.yx.listener;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.yx.constatns.RabbitMQConstatns;
import com.yx.vo.Goods;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname GoodsESQueueListener
 * @Date 2021/07/12 19:50
 * @Author by yx
 * @Description TODO:
 */
@Component
@Slf4j
public class ItemQueueListener {

    @Autowired
    private Configuration configuration;

    private String path;
    private String path1;
    private Template template;

    @PostConstruct
    public void init() throws IOException {
        //获取页面生成地址
        ClassLoader classLoader = this.getClass().getClassLoader();
        URL trmplates = classLoader.getResource("templates");
        path1 = trmplates.getPath();
        // 获取项目目录中的templates地址,避免因清空缓存所造成数据丢失的问题
        // test测试中文件路径为src\main\resources\templates
        File file = ResourceUtils.getFile("shop-11-item\\src\\main\\resources\\templates");
        path = file.getAbsolutePath();
        template = configuration.getTemplate("goodsItem.ftl");
    }

    @RabbitListener(queues = RabbitMQConstatns.ITEM_QUEUE)
    public void GoodsTohtml(String json, Channel channel, Message message) throws IOException {
        //1.获取routingkey
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        Goods goods = JSON.parseObject(json, Goods.class);
        Map<String, Object> map = new HashMap<>();

        switch (routingKey) {
            case "shop.item.insert":
                try {
                    map.put("gname", goods.getGname());
                    map.put("gprice", goods.getGprice());
                    //使用正则,切分数据
                    map.put("gpngList", goods.getGpng().split("\\|"));
                    template.process(map, new FileWriter(path + File.separator + goods.getId() + ".html"));
                    template.process(map, new FileWriter(path1 + File.separator + goods.getId() + ".html"));
                    // 手动ack
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                } catch (TemplateException e) {
                    log.error("生成item页面出现了异常", e);
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
                }
                break;
            default:
                System.out.println("无此routingKey");
                break;
        }


    }
}
